package com.web.base.wechat.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.wechat.cache.WechatAutoMessageCache;
import com.web.base.wechat.exception.WechatAutoMessageException;
import com.web.base.wechat.facade.WechatAutoMessageFacade;
import com.web.base.wechat.message.WechatAutoMessageRequest;
import com.web.base.wechat.message.WechatAutoMessageResponse;
import com.web.base.wechat.model.WechatAutoMessage;
import com.web.base.wechat.service.WechatAutoMessageService;
import com.web.base.wechat.util.WechatAutoMessageConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 微信自动回复消息接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月27日
 */
@Component
public class WechatAutoMessageProvider implements WechatAutoMessageFacade
{
	@Autowired
	private WechatAutoMessageService wechatAutoMessageService;
	@Resource
	private WechatAutoMessageCache wechatAutoMessageCache;
	
	public WechatAutoMessageProvider()
	{
	}

	@Override
	public WechatAutoMessageResponse add(WechatAutoMessageRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		WechatAutoMessage wechatAutoMessage = WechatAutoMessageConvertor.toWechatAutoMessage(request);
		wechatAutoMessage = wechatAutoMessageService.save(wechatAutoMessage);
		wechatAutoMessageCache.put(wechatAutoMessage.getId());
		WechatAutoMessageResponse response = WechatAutoMessageConvertor.toResponse(wechatAutoMessage);
		return response;
	}

	@Override
	public WechatAutoMessageResponse update(WechatAutoMessageRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		WechatAutoMessage wechatAutoMessage = WechatAutoMessageConvertor.toWechatAutoMessage(request);
		wechatAutoMessage = wechatAutoMessageService.save(wechatAutoMessage);
		wechatAutoMessageCache.put(wechatAutoMessage.getId());
		WechatAutoMessageResponse response = WechatAutoMessageConvertor.toResponse(wechatAutoMessage);
		return response;
	}

	@Override
	public WechatAutoMessageResponse get(Integer id)
	{
		if (id == null)
			throw new WechatAutoMessageException("ID不能为空");
			
		WechatAutoMessage wechatAutoMessage = wechatAutoMessageService.get(id);
		WechatAutoMessageResponse response = WechatAutoMessageConvertor.toResponse(wechatAutoMessage);
		return response;
	}

	@Override
	public Page<WechatAutoMessageResponse> listPage(WechatAutoMessageRequest request, int start, int size)
	{
		WechatAutoMessage wechatAutoMessage = WechatAutoMessageConvertor.toWechatAutoMessage(request);
		Page<WechatAutoMessage> page = new Page<>(size);
		page.setCurPage(start);
		page = wechatAutoMessageService.list(wechatAutoMessage, page);
		Page<WechatAutoMessageResponse> pageResponse = WechatAutoMessageConvertor.toResponse(page);
		return pageResponse;
	}

	@Override
	public void refresh()
	{
		try
		{
			wechatAutoMessageCache.afterPropertiesSet();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
}
